package com.yikaiye.android.yikaiye.ui.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.DataBindingHolder;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.databinding.ItemMyServiceBinding;
import com.yikaiye.android.yikaiye.model.ServiceRecordsModel;
import com.yikaiye.android.yikaiye.ui.activity.mine.CompanyDetailsActivity;
import com.yikaiye.android.yikaiye.ui.activity.service.ServiceDetailsActivity;
import com.yikaiye.android.yikaiye.utils.CommonUtil;
import com.yikaiye.android.yikaiye.utils.JumpIntent;
import com.yikaiye.android.yikaiye.utils.UiUtils;
import com.yikaiye.android.yikaiye.widget.flowLayout.FlowLayoutManager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ServiceAdapter extends BaseQuickAdapter<ServiceRecordsModel.ServiceModel, DataBindingHolder<ItemMyServiceBinding>> {

    /**
     * 0首页服务列表，1企业详情服务进度
     */
    private int tag;
    private final Context context;

    public ServiceAdapter(int tag, Context context) {
        this.tag = tag;
        this.context = context;
    }

    public ServiceAdapter(Context context) {
        this.context = context;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemMyServiceBinding> itemMyServiceBindingDataBindingHolder, int i, @Nullable ServiceRecordsModel.ServiceModel serviceModel) {
        ItemMyServiceBinding binding = itemMyServiceBindingDataBindingHolder.getBinding();
        if (serviceModel != null) {
            binding.serviceNameTv.setText(CommonUtil.checkText(serviceModel.getServiceName()));
            if (!TextUtils.isEmpty(serviceModel.getDeadline())) {
                binding.serviceNameTv.setText(CommonUtil.checkText(serviceModel.getServiceName()) + context.getString(R.string.cycle_range, serviceModel.getDeadline()));
            } else {
                binding.serviceNameTv.setText(CommonUtil.checkText(serviceModel.getServiceName()));
            }

            binding.line.setVisibility(TextUtils.isEmpty(serviceModel.getEnterpriseName()) ? View.INVISIBLE : View.VISIBLE);
            binding.companyDetailsLl.setVisibility(TextUtils.isEmpty(serviceModel.getEnterpriseName()) ? View.GONE : View.VISIBLE);

            // 服务状态：0.待办 1.在办 2.暂停 3.已办结 4.已终止
            UiUtils.setServiceStatus(serviceModel.getStatus(), binding.serviceStatusTv, context);

            final FlowLayoutManager flowLayoutManager = new FlowLayoutManager();
//            if (itemMyServiceBindingDataBindingHolder.itemView.getTag() == null) {
//                itemMyServiceBindingDataBindingHolder.itemView.setTag("item");
//            }
            binding.rv.setLayoutManager(flowLayoutManager);
            AttrAdapter adapter = new AttrAdapter();
            binding.rv.setAdapter(adapter);
            if (tag == 1) {
                String[] attrS = serviceModel.getServiceAttr().split(",");
                List<String> attrList = new ArrayList<>(Arrays.asList(attrS));
                adapter.submitList(attrList);
            } else {
                adapter.submitList(serviceModel.getServiceAttrList());
            }

            if (tag == 1) {
                binding.line.setVisibility(View.INVISIBLE);
                binding.companyDetailsLl.setVisibility(View.GONE);
//                binding.serviceCycleTv.setTypeface(Typeface.SANS_SERIF, Typeface.NORMAL);
            }
            binding.companyNameTv.setText(CommonUtil.checkText(serviceModel.getEnterpriseName()));
            binding.companyDetailsLl.setOnClickListener(view -> {
                Bundle bundle = new Bundle();
                bundle.putString(Constant.CREDIT_NO_KEY, serviceModel.getCreditNo());
                JumpIntent.jump((Activity) context, CompanyDetailsActivity.class, bundle);
            });

            binding.container.setOnClickListener(view -> {
                Bundle bundle = new Bundle();
                bundle.putString(Constant.ID_KEY, serviceModel.getId());
                bundle.putString(Constant.FROM_KEY, Constant.service);
                JumpIntent.jump((Activity) context, ServiceDetailsActivity.class, bundle);
            });

            binding.rv.setOnTouchListener((v, event) -> itemMyServiceBindingDataBindingHolder.itemView.onTouchEvent(event));
        }
    }

    @NonNull
    @Override
    protected DataBindingHolder<ItemMyServiceBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new DataBindingHolder<>(R.layout.item_my_service, viewGroup);
    }
}
